<template>
  <div>
    <el-card>
      <el-table :data="tableData" row-key="path" :highlight-current-row="true">
        <el-table-column prop="title" label="权限名称" />
        <el-table-column label="图标">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-icon size="30px">
                <component :is="mapIcon[scope.row.icon]"></component>
              </el-icon>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" round @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="你确定要删除吗" confirm-button-text="确认" cancel-button-text="取消"
              @confirm="handleDelete(scope.row)">
              <template #reference>
                <el-button type="danger" round>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" title="更新权限" @close="confirm" style="border-radius: 20px; font-weight: bold;" :center="true">
      <el-form :model="updateForm">
        <el-form-item label="权限名称: " prop="title">
          <el-input v-model="updateForm.title" autocomplete="on" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import axios from 'axios';
import { ref, onMounted, reactive } from 'vue'
import { TrendCharts, User, List, Key, Grid, OfficeBuilding, PieChart, School } from '@element-plus/icons-vue'
const mapIcon = {
  TrendCharts, User, List, Key, Grid, OfficeBuilding, PieChart, School
}
const updateForm = reactive({
  title: ''
})
const handleDelete = async (item) => {
  await axios.delete("/admin/rights",{
    data:item
  })
  console.log(item);
  await getList()
}
//用于保存点击编辑后的权限信息
const updateList = ref({})
const dialogVisible = ref(false)
const tableData = ref([])
onMounted(() => {
  getList()
})
const confirm = async () => {
  await axios.put("/admin/rights", {
    body: updateList.value,
    title: updateForm.title
  })
  // await getList()
  location.reload()
  dialogVisible.value = false
}
const handleEdit = (item) => {
  // console.log("updateItem",item);
  updateForm.title = item.title
  updateList.value = item
  dialogVisible.value = true
}
const getList = async () => {
  const res = await axios.get('/admin/rights')
  console.log('table', res.data);
  tableData.value = res.data
}
</script>
<style scoped lang='scss'>
.el-card {
  // background-color: aqua;
  border-radius: 30px;

  :deep(.el-table .cell) {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    font-weight: bold;
  }
}
</style>